package org.surelink.radius.utils;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.surelink.radius.RadiusConfig;
import org.tinyradius.packet.RadiusPacket;

@Component
public class AuthLogger {
    private static final Logger radiuslog = Logger.getLogger("radius");
    @Autowired
    private RadiusConfig config;

    public void logRequest(RadiusPacket request){
        if(config.getInt("param.logLevel")>=2){
            String msg = String.format("收到认证请求:%s;%s;detail:\n",
                    request.getAttribute("NAS-IP-Address"),
                    request.getAttribute("User-Name"),
                    request.toString());

            radiuslog.info(msg);
        }
        else
        {
            String msg = String.format("收到认证请求:%s;%s",
                    request.getAttribute("NAS-IP-Address"),
                    request.getAttribute("User-Name"));

            radiuslog.info(msg);
        }
    }

    public void logResponse(RadiusPacket request,RadiusPacket response){

        String msgtype = response.getPacketType() == RadiusPacket.ACCESS_ACCEPT?"成功":"拒绝";


        if(config.getInt("param.logLevel")>=2){
            String msg = String.format("发送认证%s响应:%s;%s;detail:%s",
                    msgtype,
                    response.getAttribute("Reply-Message"),
                    request.getAttribute("User-Name"),
                    response.toString());
            radiuslog.info(msg);
        }
        else
        {
            String msg = String.format("发送认证%s响应:%s;%s",
                    msgtype,
                    response.getAttribute("Reply-Message"),
                    request.getAttribute("User-Name"));

            radiuslog.info(msg);
        }
    }
}
